# -*-coding=UTF-8-*-

__author__ = 'tcw'
__birth__ = '2018/4/18'

import os, sys

baseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if baseDir not in sys.path:
    sys.path.append(baseDir)

import logging
import logging.config

from Program.settings import LOGGING


class MdDebug():
    def __init__(self):
        self.logger = logging.getLogger("Program")
        if not len(self.logger.handlers):
            logging.config.dictConfig(LOGGING)
        self.root_path_len = len(baseDir) + 1

    def debug(self, msg, *args):
        if args:
            msg = msg % args
        frame = sys._getframe(1)
        code = frame.f_code
        filename = code.co_filename
        if filename.startswith("/"):
            filename = code.co_filename[self.root_path_len:]
        else:
            filename = code.co_filename[2:]
        self.logger.debug("{%s:%s} %s - %s" % (filename, frame.f_lineno, code.co_name, msg))

    def info(self, msg, *args):
        if args:
            msg = msg % args
        frame = sys._getframe(1)
        code = frame.f_code
        filename = code.co_filename
        if filename.startswith("/"):
            filename = code.co_filename[self.root_path_len:]
        else:
            filename = code.co_filename[2:]
        self.logger.info("{%s:%s} %s - %s" % (filename, frame.f_lineno, code.co_name, msg))

    def warn(self, msg, *args):
        if args:
            msg = msg % args
        frame = sys._getframe(1)
        code = frame.f_code
        filename = code.co_filename
        if filename.startswith("/"):
            filename = code.co_filename[self.root_path_len:]
        else:
            filename = code.co_filename[2:]
        self.logger.warn("{%s:%s} %s - %s" % (filename, frame.f_lineno, code.co_name, msg))

    def error(self, msg, *args):
        if args:
            msg = msg % args
        frame = sys._getframe(1)
        code = frame.f_code
        filename = code.co_filename
        if filename.startswith("/"):
            filename = code.co_filename[self.root_path_len:]
        else:
            filename = code.co_filename[2:]
        formatted_log = "{%s:%s} %s - %s" % (filename, frame.f_lineno, code.co_name, msg)
        if msg == 'exception':
            self.logger.error(formatted_log, exc_info=1)
        else:
            self.logger.error(formatted_log)

    def fatal(self, msg, *args):
        if args:
            msg = msg % args
        frame = sys._getframe(1)
        code = frame.f_code
        filename = code.co_filename
        if filename.startswith("/"):
            filename = code.co_filename[self.root_path_len:]
        else:
            filename = code.co_filename[2:]
        self.logger.fatal("{%s:%s} %s - %s" % (filename, frame.f_lineno, code.co_name, msg))


Md_Debug = MdDebug()

if __name__ == "__main__":
    Md_Debug.debug("Md_Debug: This is a debug log")
    Md_Debug.info("Md_Debug: This is a info log")
    Md_Debug.warn("Md_Debug: This is a warn log")
    Md_Debug.error("Md_Debug: This is a error log")
    Md_Debug.fatal("Md_Debug: This is a critical log")
